#!/usr/bin/env python3
"""
简单的MCP客户端，用于测试网络结构化MCP服务器
"""
import json
import sys
import os
import time

def send_request(request):
    """发送请求并获取响应"""
    # 将请求写入标准输出
    print(json.dumps(request), flush=True)
    
    # 从标准输入读取响应
    response_line = sys.stdin.readline().strip()
    if response_line:
        return json.loads(response_line)
    return None

def main():
    """主函数"""
    print("MCP客户端启动...", file=sys.stderr)
    
    # 测试1: 列出工具
    print("\n测试1: 列出工具", file=sys.stderr)
    request = {
        "jsonrpc": "2.0",
        "method": "list_tools",
        "params": {},
        "id": 1
    }
    response = send_request(request)
    print(f"工具列表响应:\n{json.dumps(response, indent=2)}", file=sys.stderr)
    
    # 测试2: 调用echo工具
    print("\n测试2: 调用echo工具", file=sys.stderr)
    request = {
        "jsonrpc": "2.0",
        "method": "call_tool",
        "params": {
            "name": "async_echo",
            "arguments": {
                "message": "这是一个测试消息"
            }
        },
        "id": 2
    }
    response = send_request(request)
    print(f"Echo响应:\n{json.dumps(response, indent=2)}", file=sys.stderr)
    
    # 测试3: 获取系统信息
    print("\n测试3: 获取系统信息", file=sys.stderr)
    request = {
        "jsonrpc": "2.0",
        "method": "call_tool",
        "params": {
            "name": "system_info",
            "arguments": {}
        },
        "id": 3
    }
    response = send_request(request)
    print(f"系统信息响应:\n{json.dumps(response, indent=2)}", file=sys.stderr)
    
    # 测试4: 读取资源
    print("\n测试4: 读取资源", file=sys.stderr)
    request = {
        "jsonrpc": "2.0",
        "method": "read_resource",
        "params": {
            "uri": "system://info/resources"
        },
        "id": 4
    }
    response = send_request(request)
    print(f"资源响应:\n{json.dumps(response, indent=2)}", file=sys.stderr)
    
    print("\n所有测试完成!", file=sys.stderr)

if __name__ == "__main__":
    main()